package org.tloss.proxyserver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.tloss.proxyserver.datasource.DataSource;

public class ProxyMangager {
	private static ProxyMangager mangager = null;
	private Map<String, ProxyContextEntry> contextEntries = null;
	

	private ProxyMangager() {
	}

	public Map<String, ProxyContextEntry> getContextEntries() {
		return contextEntries;
	}

	private DataSource dataSource = null;

	public void init() {
		dataSource = DataSource.getInstanse();
		List<ProxyContextEntry> contextEntries = dataSource
				.getActiveProxyContextEntries();
		ProxyContextEntry contextEntry;
		this.contextEntries = new HashMap<String, ProxyContextEntry>();
		if (contextEntries != null) {
			List<ProxyEntry> proxyEntries;
			String context;
			Map<String, ProxyEntry> mapProxyEntries;
			ProxyEntry proxyEntry;
			List<String> hashList;
			for (int i = 0; i < contextEntries.size(); i++) {
				contextEntry = contextEntries.get(i);
				context = contextEntry.getContext();
				this.contextEntries.put(context, contextEntry);
				proxyEntries = dataSource.getActiveProxyEntries(context);
				mapProxyEntries = new HashMap<String, ProxyEntry>();
				contextEntry.setProxyEntries(mapProxyEntries);
				hashList = new ArrayList<String>();
				contextEntry.setHashList(hashList);
				if (proxyEntries != null) {
					for (int j = 0; j < proxyEntries.size(); j++) {
						proxyEntry = proxyEntries.get(j);
						hashList.add(proxyEntry.getHash());
						mapProxyEntries
								.put(proxyEntry.getHash(), proxyEntry);
					}
				}
			}
			context = null;
			proxyEntries = null;
			mapProxyEntries = null;
			proxyEntry = null;
		}
		contextEntry = null;
		contextEntries = null;
	}

	public boolean addNewContextEntry(String context, boolean enable) {
		boolean result = false;
		if (context != null && context.startsWith("/")) {
			if (!contextEntries.containsKey(context)) {
				ProxyContextEntry entry = new ProxyContextEntry();
				entry.setContext(context);
				entry.setStatus(enable ? ProxyContextEntry.ENABLE
						: ProxyContextEntry.DISABLE);
				if (dataSource.addProxyContextEntry(entry)) {
					contextEntries.put(context, entry);
					result = true;
				} else {
					// error
				}
				entry = null;
			} else {

				// existed
			}
		} else {
			// invalid context
		}
		return result;
	}

	public boolean checkContextEntryExisted(String context) {
		boolean result = false;
		if (context != null && context.startsWith("/")) {
			result = contextEntries.containsKey(context);
		}
		return result;
	}

	public boolean changeStatusContextEntry(String context, boolean enable) {
		boolean result = false;
		if (context != null && context.startsWith("/")) {
			ProxyContextEntry entry;
			if ((entry = contextEntries.get(context)) != null) {
				if (dataSource.updateProxyContextEntryStatus(context,
						enable ? ProxyContextEntry.ENABLE
								: ProxyContextEntry.DISABLE)) {
					entry.setStatus(enable ? ProxyContextEntry.ENABLE
							: ProxyContextEntry.DISABLE);
					result = true;
				} else {
					// error
				}
				entry = null;
			} else {

				// not existed
			}
		} else {
			// invalid context
		}
		return result;
	}

	public boolean addNewProxyEntry(ProxyEntry proxyEntry) {
		boolean result = false;
		String cotext = proxyEntry.getProxyContext();
		if (cotext != null && cotext.startsWith("/")) {
			ProxyContextEntry entry;
			if ((entry = contextEntries.get(cotext)) != null) {
				Map<String, ProxyEntry> proxyEntries = entry.getProxyEntries();
				String hash = proxyEntry.getHash();
				if (proxyEntries != null && !proxyEntries.containsKey(hash)
						&& dataSource.addProxyEntry(proxyEntry)) {
					proxyEntries.put(hash, proxyEntry);
					entry.getHashList().add(hash);
					result = true;
				} else {
					// error
				}
				proxyEntries = null;
				hash = null;
			} else {
				// not existed
			}
			entry = null;
		} else {
			// invalid context
		}
		cotext = null;
		return result;
	}

	public boolean updateProxyEntryStatus(String context, String hash,
			int status) {
		boolean result = false;
		if (context != null && context.startsWith("/")) {
			ProxyContextEntry entry;
			if ((entry = contextEntries.get(context)) != null) {
				Map<String, ProxyEntry> proxyEntries = entry.getProxyEntries();
				ProxyEntry proxyEntry;
				if (proxyEntries != null
						&& (proxyEntry = proxyEntries.get(hash)) != null) {
					if (dataSource.updateProxyEntryStatus(hash, status)) {
						proxyEntry.setStatus(status);

						result = true;
					} else {
						// error
					}
					proxyEntries = null;
					proxyEntry = null;
				} else {
					// error
				}
			} else {

				// not existed
			}
			entry = null;
		} else {
			// invalid context
		}
		return result;
	}

	public Map<String, ProxyEntry> getProxyEntries(String context) {
		Map<String, ProxyEntry> result = null;
		if (context != null && context.startsWith("/")) {
			ProxyContextEntry entry;
			if ((entry = contextEntries.get(context)) != null) {
				return entry.getProxyEntries();
			}
		}
		return result;
	}

	public static synchronized ProxyMangager getInstanse() {
		if (mangager == null) {
			mangager = new ProxyMangager();
		}
		return mangager;
	}
}
